package de.breitbeil.jbooking.ui.view.tablecellrenderer;

import java.awt.Component;
import java.util.Comparator;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import de.breitbeil.jbooking.domain.Person;

public class PersonTableCellRenderer extends DefaultTableCellRenderer implements Comparator<Person> {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        
        if(value instanceof Person) {
            setText( getString((Person)value) );
        }
        
        return this;
    }
    
    public String getString(Person person) {
        if(person == null) {
            return "";
        }
        
        String firstname = (person.getFirstname() == null)? "" : person.getFirstname();
        String lastname = (person.getLastname() == null)? "" : person.getLastname();
        
        return lastname + " " + firstname;
    }

    public int compare(Person o1, Person o2) {
        return getString(o1).compareTo(getString(o2));
    }
}
